package com.ruoyi.web.controller.wechat;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.wechat.domain.WechatUserPointsRecord;
import com.ruoyi.wechat.service.IWechatUserPointsRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 微信用户积分记录Controller
 *
 * @author ruoyi
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/wechat/points/record")
@RequiredArgsConstructor
public class WechatUserPointsRecordController extends BaseController {

    private final IWechatUserPointsRecordService wechatUserPointsRecordService;
    private final TokenService tokenService;

    /**
     * 查询微信用户积分记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WechatUserPointsRecord wechatUserPointsRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<WechatUserPointsRecord> list = wechatUserPointsRecordService.selectWechatUserPointsRecordList(wechatUserPointsRecord);
        list.forEach(item -> wechatUserPointsRecordService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 导出微信用户积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('points:record:export')")
    @Log(title = "微信用户积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WechatUserPointsRecord wechatUserPointsRecord) {
        List<WechatUserPointsRecord> list = wechatUserPointsRecordService.selectWechatUserPointsRecordList(wechatUserPointsRecord);
        ExcelUtil<WechatUserPointsRecord> util = new ExcelUtil<WechatUserPointsRecord>(WechatUserPointsRecord.class);
        util.exportExcel(response, list, "微信用户积分记录数据");
    }

    /**
     * 获取微信用户积分记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wechatUserPointsRecordService.selectWechatUserPointsRecordById(id));
    }

    /**
     * 新增微信用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:add')")
    @Log(title = "微信用户积分记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WechatUserPointsRecord wechatUserPointsRecord) {
        return toAjax(wechatUserPointsRecordService.insertWechatUserPointsRecord(wechatUserPointsRecord));
    }

    /**
     * 修改微信用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:edit')")
    @Log(title = "微信用户积分记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WechatUserPointsRecord wechatUserPointsRecord) {
        return toAjax(wechatUserPointsRecordService.updateWechatUserPointsRecord(wechatUserPointsRecord));
    }

    /**
     * 删除微信用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:remove')")
    @Log(title = "微信用户积分记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wechatUserPointsRecordService.deleteWechatUserPointsRecordByIds(ids));
    }
}
